<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <!-- <el-form-item>
        <el-input v-model="dataForm.key" placeholder="用户名" clearable></el-input>
      </el-form-item> -->
      <el-form-item>
        <el-button class="el-icon-plus" @click="addTabModel = true" type="primary"> 新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="标题"
        sortable>
      </el-table-column>
      <el-table-column
        prop="power"
        header-align="center"
        align="center"
        label="权重"
        sortable>
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="状态"
        sortable>
        <template slot-scope="scope">
          <!-- <el-tag v-if="scope.row.status === 0" size="small" type="danger">禁用</el-tag> -->
          <el-tag v-if="scope.row.status == 1" type="success" size="small">显示</el-tag>
          <el-tag v-if="scope.row.status == 2" size="small">隐藏</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status == 2" type="primary" size="small" @click="upTable(scope.row)">显示</el-button>
          <el-button v-if="scope.row.status == 1" type="danger" size="small" @click="downTable(scope.row)">隐藏</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 显示 模态框 -->
    <el-dialog
      title="提示"
      :visible.sync="upTableModel"
      width="30%"
      center>
      <div style="text-align:center;font-size:22px">确定显示[{{this.name}}]栏目吗?</div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="upTableModel = false">取 消</el-button>
        <el-button type="primary" @click="saveUpTableModel">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 隐藏 模态框 -->
    <el-dialog
      title="提示"
      :visible.sync="downTableModel"
      width="30%"
      center>
      <div style="text-align:center;font-size:22px">确定隐藏[{{this.name}}]栏目吗?</div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="downTableModel = false">取 消</el-button>
        <el-button type="primary" @click="saveDownTableModel">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 新增 模态框 -->
    <el-dialog
      title="新增tab栏"
      :visible.sync="addTabModel"
      width="450px"
      center>
      <div class="detailsMain">
        <div class="detailsMain-item">
          <div class="detailsMain-item-l">标题</div>
          <div class="detailsMain-item-r">
            <el-input v-model="tabname" placeholder="请输入标题"></el-input>
          </div>
        </div>
        <div class="detailsMain-item">
          <div class="detailsMain-item-l">权重</div>
          <div class="detailsMain-item-r">
            <el-input v-model="power" type="number" min="0" placeholder="请输入权重"></el-input>
          </div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addTabModel = false">取 消</el-button>
        <el-button type="primary" @click="addNewTab">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],

        addTabModel: false,//新增 模态框
        tabname:'',//tabname
        power:'',//权重
        upTableModel: false,//显示 模态框
        downTableModel: false,//隐藏 模态框
        id:'',//显示 隐藏id
        name:'',//显示 隐藏标题

      }
    },
    activated () {
      this.getDataList()
    },
    methods: {
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/common/tab/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            console.log(data)
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
       // 显示按钮
      upTable (arow) {
        this.id = arow.id
        this.name = arow.name
        this.upTableModel = true
        console.log(arow.id)
      },
      saveUpTableModel () {
        this.getAudit(this.id,1)
      },
      // 隐藏按钮
      downTable (arow) {
        this.id = arow.id
        this.name = arow.name
        this.downTableModel = true
      },
      saveDownTableModel () {
        this.getAudit(this.id,2)
      },
      //显示 1  和 隐藏 2
      getAudit(id,status){
         this.$http({
            url: this.$http.adornUrl('/common/tab/update'),
            method: 'post',
            data: this.$http.adornData({
              'id': id,
              'status': status
              })
          }).then(({data}) => {
                  console.log(data)
              if (data && data.code == 0) {
                  this.upTableModel = false
                  this.downTableModel = false
                  this.$message.success("成功!")
                  this.getDataList()
              } else {
                  this.upTableModel = false
                  this.downTableModel = false
                  this.$message.error(data.msg)
                  this.getDataList()
              }
          })
      },
      // 新增模态框  确定按钮
      addNewTab () {
        if(this.power==''||this.tabname==''){
          this.$message.error('内容不能为空!')
        }else{
          this.$http({
            url: this.$http.adornUrl('/common/tab/save'),
            method: 'post',
            data: this.$http.adornData({
              'power': this.power,
              'name': this.tabname,
              'status': 1
            })
          }).then(({data}) => {
            console.log(data)
            if (data && data.code === 0) {
              this.$message.success("新增成功!")
              this.addTabModel = false
              this.getDataList()
            } else {
              this.$message.error(data.msg)
              this.addTabModel = false
              this.getDataList()
            }
          })
        }
      }
    }
  }
</script>

<style scoped>
  .detailsMain-item{
    display: flex;
    margin-bottom: 15px
  }
  .detailsMain-item-l{
    width: 100px;
    line-height: 2.5;
  }
  .detailsMain-item-r{
    flex: 1;
    word-break:break-all;
  }
</style>
